import pygame
from setting import Setting


class Ship:
    def __init__(self, ai_game) -> None:
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()
        self.settings: Setting = ai_game.settings
        self.image = pygame.image.load('images/ship1.png')
        self.image = pygame.transform.scale(
            self.image, self.settings.ship_size)
        self.rect = self.image.get_rect()
        self.rect.midbottom = self.screen_rect.midbottom
        self.move_right = False
        self.move_left = False
        self.move_up = False
        self.move_down = False
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.move_right and self.rect.right < self.screen_rect.right + self.rect.width / 3:
            self.x += self.settings.ship_speed
        elif self.move_left and self.rect.left > - self.rect.width / 3:
            self.x -= self.settings.ship_speed
        elif self.move_up and self.rect.top > - self.rect.height / 3:
            self.y -= self.settings.ship_speed
        elif self.move_down and self.rect.bottom < self.screen_rect.bottom + self.rect.height / 3:
            self.y += self.settings.ship_speed

        self.rect.x = self.x
        self.rect.y = self.y

    def center_ship(self):
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)
        self.y = self.screen_rect.bottom - self.rect.height
